<?php
namespace app\app\model;
use app\app\util\BlockUtils;
use think\Model;
use think\Db;

class TaskModel extends Model{

    public $user_id;
    protected $table = "a_task";

    public function getList(){
        $data = $this->field('id,title,reword,keyword,complete_num')->select();
        //有任务
        if( $data ){
            foreach($data as $k => $v){
                //分享任务
                if($v['keyword'] == "share"){
                    $share = BlockUtils::getShare($this->user_id);
                    if($share >= $v['complete_num']){
                        //查询是否已领取奖励
                        $start = strtotime(date('Y-m-d')." 00:00:01");
                        $end   = strtotime(date('Y-m-d')." 23:59:59");
                        $task = Db::name('my_task')->where('user_id','eq',$this->user_id)->where('task_id','eq',$v['id'])->where('create_time','between',[$start,$end])->find();
                        if( $task ){
                            $data[$k]['is_receive'] = 1;
                        }else{
                            $data[$k]['is_receive'] = 0;
                        }
                    }else{
                        $data[$k]['is_receive'] = 0;
                    }
                    $data[$k]['my_num'] = $share;
                }elseif($v['keyword'] == "answer"){
                    $answer = BlockUtils::getAnswer($this->user_id);
                    if($answer >= $v['complete_num']){
                        //查询是否已领取奖励
                        $start = strtotime(date('Y-m-d')." 00:00:01");
                        $end   = strtotime(date('Y-m-d')." 23:59:59");
                        $task = Db::name('my_task')->where('user_id','eq',$this->user_id)->where('task_id','eq',$v['id'])->where('create_time','between',[$start,$end])->find();
                        if( $task ){
                            $data[$k]['is_receive'] = 1;
                        }else{
                            $data[$k]['is_receive'] = 0;
                        }
                    }else{
                        $data[$k]['is_receive'] = 0;
                    }
                    $data[$k]['my_num'] = $answer;
                }elseif($v['keyword'] == "answers"){
                    $answer = BlockUtils::getAnswer($this->user_id);
                    $user = Db::name('users')->where('id','eq',$this->user_id)->find();
                    if($user['answers_num'] == 0){
                        $data[$k]['is_recieve'] = 0;
                        $data[$k]['my_num'] = $answer;
                    }else{
                        $num = $answer - $user['answers_num']*10;
                        if($num >= 10){
                            $data[$k]['is_recieve'] = 0;
                        }else{
                            $data[$k]['is_recieve'] = 1;
                        }
                        $data[$k]['my_num'] = $num;
                    }
                }
            }
        }else{
            $data = array();
        }
        return $data;
    }

    /**
     * @param $key
     */
    public function getOne($key){
        return $this->field("id,title,reword,complete_num,description")->where('keyword','eq',$key)->find();
    }

    /**
     * 插入领奖记录
     * @param $arr
     */
    public function saveData($arr){
        Db::name('my_task')->insert($arr);
        return true;
    }


}